package com.yj.suncity.activity.shouye;

import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.yj.suncity.R;
import com.yj.suncity.async.AsyncTaskUtil;
import com.yj.suncity.async.AsyncTaskUtil.ResultCallBack;
import com.yj.suncity.constant.URLCon;
import com.yj.suncity.utis.DialogUtils;
import com.yj.suncity.utis.ToastUtil;

public class HongbaoDialog extends Activity{
	private String hongbaoMsg;
	private String hongbao;
	private String name;
	private Button lingquBtn;
	private Button fangqiBtn;
	private String recordId;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_hongbao);
        
        Intent intent = getIntent();
        
        name = intent.getStringExtra("share_typename");
        hongbao = intent.getStringExtra("hongbao");
        recordId = intent.getStringExtra("id");
        
        TextView title = (TextView)findViewById(R.id.hongbao_title);
        title.setText("恭喜您\n抢到<"+name+">卡券");
        TextView infoText = (TextView)findViewById(R.id.hongbao_text);
        infoText.setText(hongbao);
        lingquBtn = (Button)findViewById(R.id.lingqu_btn);
        fangqiBtn = (Button)findViewById(R.id.fangqi_btn);
        
        lingquBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				guanggao("share_confirm",1);
			}
		});
        fangqiBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				guanggao("share_cancel",1);
			}
		});
//        TextView msgText = (TextView)findViewById(R.id.hongbao_msg);
//        msgText.setText(hongbaoMsg);
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(keyCode==KeyEvent.KEYCODE_BACK){
			guanggao("share_cancel",2);
		}
		return super.onKeyDown(keyCode, event);
	}
	private void guanggao(String type,final int flag){
		String path;
		path = URLCon.广告 + "?action="+type+"&record_id=" + recordId;
		Log.d("", path);
		AsyncTaskUtil asyncTaskUtil = new AsyncTaskUtil(HongbaoDialog.this, null, null,
				false, null, true, "", new ResultCallBack() {
					@Override
					public void JSONResult(JSONObject jsonObject) {
						
						if (jsonObject.optString("success").equals("1")) {
							ToastUtil.ShowToast(HongbaoDialog.this,
									jsonObject.optString("msg"));						
								HongbaoDialog.this.finish();
						}else{
							ToastUtil.ShowToast(HongbaoDialog.this, jsonObject.optString("msg"));						
								HongbaoDialog.this.finish();
						}
					}
				});
		asyncTaskUtil.execute(path);
	}
	
}
